package it.aupapp.web.style;

import it.aupapp.model.Color;
import it.aupapp.model.Style;

import java.util.ArrayList;
import java.util.List;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Query;

public class StyleDao {
	private final static String BACKGROUND_COLOR = "BACKGROUND_COLOR";
	private final static String TEXT_COLOR = "TEXT_COLOR";
	private final static String TAB_BACKGROUND_COLOR = "TAB_BACKGROUND_COLOR";
	private final static String TAB_TEXT_COLOR = "TAB_TEXT_COLOR";

	public List<String> loadAllNames() {
		Query query = new Query("Style");

		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
		Iterable<Entity> entities = datastore.prepare(query).asIterable();
		
		List<String> styles = new ArrayList<String>();
		for (Entity entity : entities) {
			styles.add(entity.getKey().getName());
		}

		return styles;
	}

	public List<Style> loadAll() {

		Query query = new Query("Style");

		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
		Iterable<Entity> entities = datastore.prepare(query).asIterable();
		
		List<Style> styles = new ArrayList<Style>();
		for (Entity entity : entities) {
			styles.add(toStyle(entity));
		}
		
		return styles;
	}

	public Style load(String name) {
		Key key = KeyFactory.createKey("Style", name);
		Query query = new Query(key);

		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
		Entity entity = datastore.prepare(query).asSingleEntity();

		
		
		return toStyle(entity);
	}

	public void save(Style style) {
		Entity entity = toEntity(style);
		
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
		
		datastore.put(entity);
	
		
	}
	public void remove(String name) {
		Key key = KeyFactory.createKey("Style", name);
		
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
		
		datastore.delete(key);
	}
	
	
	
	
	
	// utils
	
	private Style toStyle(Entity entity) {
		Style style = new Style();
		style.setName(entity.getKey().getName());
		if(entity.hasProperty(BACKGROUND_COLOR)) style.setBackgroundColor(toColor((int[]) entity.getProperty(BACKGROUND_COLOR)));
		if(entity.hasProperty(TEXT_COLOR)) style.setTextColor(toColor((int[]) entity.getProperty(TEXT_COLOR)));
		if(entity.hasProperty(TAB_TEXT_COLOR)) style.setTabBackgroundColor(toColor((int[]) entity.getProperty(TAB_BACKGROUND_COLOR)));
		if(entity.hasProperty(TAB_BACKGROUND_COLOR)) style.setTabTextColor(toColor((int[]) entity.getProperty(TAB_TEXT_COLOR)));
		
		return style;
	}

	private Entity toEntity(Style style) {
		Key key = KeyFactory.createKey("Style", style.getName());
		Entity entity = new Entity(key);

		if(style.getBackgroundColor() != null) entity.setProperty(BACKGROUND_COLOR, toColorArray(style.getBackgroundColor()));
		if(style.getTextColor() != null) entity.setProperty(TEXT_COLOR, toColorArray(style.getTextColor()));
		if(style.getTabBackgroundColor() != null) entity.setProperty(TAB_BACKGROUND_COLOR, toColorArray(style.getTabBackgroundColor()));
		if(style.getTabTextColor() != null) entity.setProperty(TAB_TEXT_COLOR, toColorArray(style.getTabTextColor()));

		return entity;
	}

	private int[] toColorArray(Color color) {
		return new int[] { color.R, color.G, color.B, color.A };
	}

	private Color toColor(int[] color){
		return Color.rgb(color[0], color[1], color[2], color[3]);
	}

	
}
